package cn.iocoder.yudao.module.biz.controller.admin.expand.targets;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.ExpandTargetViewDO;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import javax.servlet.http.HttpServletResponse;
import javax.validation.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.biz.controller.admin.expand.targets.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.ExpandTargetDO;
import cn.iocoder.yudao.module.biz.service.expand.targets.ExpandTargetService;

import java.io.IOException;
import java.util.List;

@Tag(name = "管理后台 - 拓展目标")
@RestController
@RequestMapping("/san/biz-expand-target")
@Validated
public class ExpandTargetController {

    @Resource
    private ExpandTargetService expandTargetService;

    @PostMapping("/create")
    @Operation(summary = "创建拓展目标")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target:create')")
    public CommonResult<Long> createExpandTarget(@Valid @RequestBody ExpandTargetSaveReqVO createReqVO) {
        createReqVO.setOwnerUserId(getLoginUserId());
        return success(expandTargetService.createExpandTarget(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拓展目标")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target:update')")
    public CommonResult<Boolean> updateExpandTarget(@Valid @RequestBody ExpandTargetSaveReqVO updateReqVO) {
        expandTargetService.updateExpandTarget(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拓展目标")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target:delete')")
    public CommonResult<Boolean> deleteExpandTarget(@RequestParam("id") Long id) {
        expandTargetService.deleteExpandTarget(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除拓展目标")
                @PreAuthorize("@ss.hasPermission('san:biz-expand-target:delete')")
    public CommonResult<Boolean> deleteExpandTargetList(@RequestParam("ids") List<Long> ids) {
        expandTargetService.deleteExpandTargetListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拓展目标")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target:query')")
    public CommonResult<ExpandTargetRespVO> getExpandTarget(@RequestParam("id") Long id) {
        ExpandTargetDO expandTarget = expandTargetService.getExpandTarget(id);
        return success(BeanUtils.toBean(expandTarget, ExpandTargetRespVO.class));
    }

   /* @GetMapping("/page")
    @Operation(summary = "获得拓展目标分页")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target:query')")
    public CommonResult<PageResult<ExpandTargetRespVO>> getExpandTargetPage(@Valid ExpandTargetPageReqVO pageReqVO) {
        PageResult<ExpandTargetDO> pageResult = expandTargetService.getExpandTargetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpandTargetRespVO.class));
    }*/

    @GetMapping("/export-excel")
    @Operation(summary = "导出拓展目标 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpandTargetExcel(@Valid ExpandTargetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpandTargetDO> list = expandTargetService.getExpandTargetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拓展目标.xls", "数据", ExpandTargetRespVO.class,
                        BeanUtils.toBean(list, ExpandTargetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拓展目标分页")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target:query')")
    public CommonResult<PageResult<ExpandTargetRespVO>> getExpandTargetPage(@Valid ExpandTargetPageReqVO pageReqVO) {
        PageResult<ExpandTargetViewDO> pageResult = expandTargetService.getAllExpandTargetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpandTargetRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得全部拓展目标")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target:query')")
    public CommonResult<List<ExpandTargetRespVO>> getExpandTargetList(@Valid ExpandTargetPageReqVO pageReqVO) {
        return success(BeanUtils.toBean(expandTargetService.getExpandTargetList(pageReqVO), ExpandTargetRespVO.class));
    }

}